//
//  LNNavigationControllerStack.m
//  LNBeeHive
//
//  Created by wwww on 2019/9/30.
//  Copyright © 2019 淘发现1iOS. All rights reserved.
//

#import "LNNavigationControllerStack.h"
#import "LNViewModelServicesImpl.h"
#import "LNMainController.h"


@interface LNNavigationControllerStack()
@property (nonatomic,strong) id<LNNavigationProtocol> services;
@property (nonatomic,strong) NSMutableArray *navigationControllers;
@end

@implementation LNNavigationControllerStack

-(instancetype)initWithServices:(id<LNNavigationProtocol>)services{
    
    self = [super init];
    if (self) {
        self.services = services;
        self.navigationControllers = [[NSMutableArray alloc] init];
        [self registerNavigationHooks];
    }
    return self;
}

- (void)pushNavigationController:(UINavigationController *)navigationController;{
    
    if ([self.navigationControllers containsObject:navigationController]) return;
    [self.navigationControllers addObject:navigationController];
}

- (UINavigationController *)popNavigationController{
    
    UINavigationController  *navigationController = self.navigationControllers.lastObject;
    [self.navigationControllers removeLastObject];
    
    return navigationController;
}

- (UINavigationController *)topNavigationController{
    
    return self.navigationControllers.lastObject;
}

- (void)registerNavigationHooks {
    
    @weakify(self)
    [[(NSObject *)self.services rac_signalForSelector:@selector(pushViewModel:animated:)] subscribeNext:^(RACTuple *tuple) {
        @strongify(self)
        
        UIViewController *vieweController = (UIViewController *)[LNRouter.sharedInstance viewContollerForViewModel:tuple.first];
        vieweController.hidesBottomBarWhenPushed = YES;
        [self.navigationControllers.lastObject pushViewController:vieweController animated:[tuple.second boolValue]];
    }];
    
    [[(NSObject *)self.services rac_signalForSelector:@selector(popViewModelAnimated:)] subscribeNext:^(RACTuple *tuple) {
        @strongify(self)
        [self.navigationControllers.lastObject popViewControllerAnimated:[tuple.first boolValue]];
    }];
    
    [[(NSObject *)self.services rac_signalForSelector:@selector(popToRootViewModelAnimated:)] subscribeNext:^(RACTuple *tuple) {
        
        @strongify(self)
        [self.navigationControllers.lastObject popToRootViewControllerAnimated:[tuple.first boolValue]];
    }];
    
    [[(NSObject *)self.services rac_signalForSelector:@selector(presentViewModel:animated:)] subscribeNext:^(RACTuple *tuple) {
        
        @strongify(self)
        UIViewController *viewController = (UIViewController *)[LNRouter.sharedInstance viewContollerForViewModel:tuple.first];
        UINavigationController *presentingViewController = self.navigationControllers.lastObject;
        
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
        [self pushNavigationController:navController];
        [presentingViewController presentViewController:navController animated:[tuple.second boolValue] completion:tuple.third];
    }];
    
    [[(NSObject *)self.services rac_signalForSelector:@selector(dismissViewModelAnimated:)] subscribeNext:^(RACTuple *tuple) {
        @strongify(self)
        [self popNavigationController];
        [self.navigationControllers.lastObject dismissViewControllerAnimated:[tuple.first boolValue] completion:tuple.second];
    }];
    
    [[(NSObject *)self.services
      rac_signalForSelector:@selector(resetRootViewModel:)]
     subscribeNext:^(RACTuple *tuple) {
         @strongify(self)
         [self.navigationControllers removeAllObjects];
         /// VM映射VC
         UIViewController *viewController = (UIViewController *)[LNRouter.sharedInstance viewContollerForViewModel:tuple.first];
         if (![viewController isKindOfClass:[LNMainController class]]) {
             viewController = [[UINavigationController alloc] initWithRootViewController:viewController];
             [self pushNavigationController:(UINavigationController *)viewController];
         }
         LNSharedAppDelegate.window.rootViewController = viewController;
     }];
}
@end
